﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Assertions;

namespace Nirvana
{
    /// <summary>
    /// 用于UIVariable脚本的变量在Bind对象上如何显示
    /// </summary>
    [CustomPropertyDrawer(typeof(VariableNameAttribute))]
    public sealed class VariableNamePropertyDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect pos, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(pos, label, property);
            UIVariableBind uivariableBind = (UIVariableBind)property.serializedObject.targetObject;
            if(uivariableBind != null)
            {
                var variableTable = uivariableBind.VariableTable;
                if(variableTable!= null)
                {
                    string[] variableNames = variableTable.GetVariableNames();
                    if (variableNames != null)
                    {
                        this.DrawVariable(pos, property, label, variableTable, variableNames);
                    }
                    else
                    {
                        GUI.enabled = false;
                        EditorGUI.PropertyField(pos, property);
                        GUI.enabled = true;
                    }
                }
                else
                {
                    GUI.enabled = false;
                    EditorGUI.PropertyField(pos, property);
                    GUI.enabled = true;
                }
            }
            else
            {
                GUI.enabled = false;
                EditorGUI.PropertyField(pos, property);
                GUI.enabled = true;
            }
            EditorGUI.EndProperty();
        }


        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            return 1f * EditorGUIUtility.singleLineHeight;
        }

        private void DrawVariable(Rect pos, SerializedProperty property, GUIContent label, UIVariableTable variableTable, string[] variableNames)
        {
            var attribute = (VariableNameAttribute)base.attribute;
            Assert.IsNotNull<VariableNameAttribute>(attribute);
            List<string> list = new List<string>();
            foreach (string n in variableNames)
            {
                UIVariable uivariable = variableTable.FindVariable(n);
                Assert.IsNotNull<UIVariable>(uivariable, "The variable is null.");
                if (attribute.IsValid(uivariable.VariableType))
                {
                    list.Add(n);
                }
            }

            Rect rect = new Rect(pos.x, pos.y, pos.width * 0.39f, pos.height);
            Rect rect2 = new Rect(pos.x + pos.width * 0.39f, pos.y, pos.width * 0.61f, pos.height);
            EditorGUI.PrefixLabel(rect, GUIUtility.GetControlID(FocusType.Passive), label);
            int num = list.IndexOf(property.stringValue);
            list.Add("None");
            int num2 = EditorGUI.Popup(rect2, num, list.ToArray());
            if (num2 != num && num2 >= 0)
            {
                if (num2 < list.Count - 1)
                {
                    property.stringValue = list[num2];
                }
                else
                {
                    property.stringValue = string.Empty;
                }
            }
        }

    }
}

